package pt.capgemini.poc.services.jaxasync;

import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.api.addressing.AddressingVersion;
import com.sun.xml.ws.api.addressing.WSEndpointReference;
import com.sun.xml.ws.api.message.HeaderList;

import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.JAXWSProperties;

import com.sun.xml.ws.developer.WSBindingProvider;

import javax.annotation.Resource;

import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebService;

import javax.xml.ws.WebServiceContext;
import javax.xml.ws.soap.Addressing;

import pt.capgemini.poc.clients.jaxasync.HelloAsyncService;

@WebService(name= "HelloAsync", serviceName = "HelloAsyncService", portName = "HelloAsyncServicePort")
@Addressing
public class HelloAsyncImpl implements HelloAsync {

    @Resource
    WebServiceContext context;

    @Oneway
    @WebMethod
    public void sayHello(String str) {          
        HeaderList hl = (HeaderList) context.getMessageContext().get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
         
        // gets the addressing informations in the SOAP header
        WSEndpointReference reference = hl.getReplyTo(AddressingVersion.W3C, 
                                                      SOAPVersion.SOAP_11);
        String messageId = hl.getMessageID(AddressingVersion.W3C, 
                                           SOAPVersion.SOAP_11);
   
        HelloAsyncService helloAsyncService = new HelloAsyncService();
        pt.capgemini.poc.clients.jaxasync.HelloAsync helloAsync = helloAsyncService.getHelloAsyncServicePort();
        WSBindingProvider bp = (WSBindingProvider) helloAsync;
 
        bp.setAddress( reference.getAddress());
        bp.setOutboundHeaders(Headers.create(AddressingVersion.W3C.relatesToTag, messageId));


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // DO NOTHING    
        }
        
        helloAsync.sayHelloCallback("hello you [" +  reference.getAddress() + "]");
    }

    @Oneway
    @WebMethod
    public void sayHelloCallback(String ret) {
    }
}
